<?php
/**
 *调拨仓库模型;主要用于采购需求调拨
 */
class Cron_Model_Backup_TransferWarehouse extends Model_Base {
	private $_productSalesModel;
	private $_productWarehouseStockModel;
	private $_transferApplyModel;
	private $_warehouseProductModel;

	//可以申请调拨的仓库
	const CAN_APPLY_TRANSFER_WAREHOUSE = [
		'评测仓' => 'PCWH',
		'ebay深圳仓' => 'CNSZ',
		'分销仓' => 'YB',
		'电子仓' => 'DZWH',
		'服装仓' => 'SZ',
	];

	public function __construct() {
		$this->db = $this->db();
	}

	/**
	 * 可以申请调拨的需求仓库
	 * @param  [type] $transferToWarehouse [description]
	 * @return [type]                      [description]
	 */
	public function canApplyTransferWarehouse() {
		return array_values(self::CAN_APPLY_TRANSFER_WAREHOUSE);
	}

	/**
	 *获取 根据调拨优先级排序后的可调拨需求仓库
	 * @return [type] [description]
	 */
	public function getCanTransferWarehouseOrderByPrior() {

		return [
			self::CAN_APPLY_TRANSFER_WAREHOUSE['评测仓'],
			self::CAN_APPLY_TRANSFER_WAREHOUSE['ebay深圳仓'],
			self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'],
			self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'],
			self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'],
		];
	}

	/**
	 * 获取根据调拨需求仓获取可以调拨的出库仓
	 * @param  [type] $TransferInWarehouse 有调拨需求的仓库
	 * @return mix 成功返回出库仓的仓库代码，未找到返回false
	 */
	public function getTransferFromWarehouse($transferToWarehouse, $goodsSn) {
		$transferFromWarehouse = null;
		$transferToWarehouse = strtoupper(trim($transferToWarehouse));
		if (!in_array($transferToWarehouse, self::CAN_APPLY_TRANSFER_WAREHOUSE)) {
			return false;
		}

		switch ($transferToWarehouse) {

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['评测仓']:
			$transferFromWarehouse = self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'];
			break;

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['ebay深圳仓']:
			$transferFromWarehouse = self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'];
			break;

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']:
			$ybWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']);
			$dzwhWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']);
			$transferFromWarehouse = ($ybWarehouseTransNum > $dzwhWarehouseTransNum) ? self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'] : self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'];
			break;
			break;

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']:
			$szWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']);
			$dzwhWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']);

			$transferFromWarehouse = ($szWarehouseTransNum >= $dzwhWarehouseTransNum) ? self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'] : self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'];
			break;
		case self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']:
			$ybWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']);
			$szWarehouseTransNum = $this->allowTransferQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']);
			$transferFromWarehouse = ($ybWarehouseTransNum > $szWarehouseTransNum) ? self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'] : self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'];
			break;
		default:
			break;
		}
		return is_null($transferFromWarehouse) ? false : $transferFromWarehouse;
	}

	/**
	 * 仓库是否允许调拨某个商品
	 * @param  string  $warehouse [description]
	 * @param  [type]  $goodsSn               [description]
	 * @return boolean                        [description]
	 */
	public function isAllowTransfer(string $warehouse, $goodsSn) {
		$warehouse = strtoupper(trim($warehouse));
		$this->_warehouseProductModel || $this->_warehouseProductModel = new Application_Model_WarehouseProduct();
		//服装，电子仓非新品允许调拨
		if (in_array($warehouse, [self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']])) {
			if (!$this->_warehouseProductModel->isNewProduct($goodsSn, $warehouse)) {
				return true;
			}
		} elseif ($warehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']) {
			//调出仓库为分销仓时，sku在CB平台上架时间需超过60天。
			$webUpShelfWhere = $this->db->quoteInto("goods_sn=?", $goodsSn);
			$before60Days = date('Y-m-d H:i:s', strtotime("-60 day"));
			$webUpShelfWhere .= " AND " . $this->db->quoteInto("shelf_time <=?", $before60Days);
			return $this->db->fetchOne("select count(*) from p_product where " . $webUpShelfWhere);
		} else {
			return false;
		}
	}

	/**
	 * 计算商品在仓库可以调拨的数量
	 * @param  [type] $goodsSn               请求调拨商品
	 * @param  string $transferFromWarehouse 调拨出库仓
	 * @param  string $transferToWarehouse   调拨入库仓
	 * @return int 仓库可以调拨商品的数量
	 */
	public function allowTransferQuantity($goodsSn, string $transferFromWarehouse, string $transferToWarehouse) {
		$this->_transferApplyModel || $this->_transferApplyModel = new Cron_Model_Backup_StockTransferApply();
		$this->_productSalesModel || $this->_productSalesModel = new Application_Model_ProductSales();
		$this->_productWarehouseStockModel || $this->_productWarehouseStockModel = new Application_Model_ProductWarehouseStock();
		$canTransferQuantity = 0;
		if (!$this->isAllowTransfer($transferFromWarehouse, $goodsSn)) {
			return $canTransferQuantity;
		}

		$warehouseAvailableStock = $this->_productWarehouseStockModel->getAvailableStock($goodsSn, $transferFromWarehouse);
		$productThirtyDaySales = $this->_productSalesModel->goodsRealSalesQuantity($goodsSn, $transferFromWarehouse, Application_Model_ProductSales::THIRTY_DAY);

		switch ($transferToWarehouse) {
		case self::CAN_APPLY_TRANSFER_WAREHOUSE['评测仓']:
			$canTransferQuantity = $warehouseAvailableStock;
			break;
		case self::CAN_APPLY_TRANSFER_WAREHOUSE['ebay深圳仓']:
			$canTransferQuantity = $warehouseAvailableStock;
			break;
		case self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']:
			if ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']) {
				$canTransferQuantity = $warehouseAvailableStock - $productThirtyDaySales * 0.5;
			} elseif ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']) {
				$hasOrderTransferAmount = $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['评测仓']);
				$canTransferQuantity = $warehouseAvailableStock - $productThirtyDaySales * 0.5 - $hasOrderTransferAmount;
			}

			break;

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']:
			if ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']) {
				$hasOrderTransferAmount = $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['ebay深圳仓']);
				$canTransferQuantity = $warehouseAvailableStock - $productThirtyDaySales - $hasOrderTransferAmount;
			} elseif ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']) {
				$hasOrderTransferAmount = $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']);
				$canTransferQuantity = $warehouseAvailableStock - $productThirtyDaySales * 0.5 - $hasOrderTransferAmount;
			}
			break;

		case self::CAN_APPLY_TRANSFER_WAREHOUSE['服装仓']:
			if ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']) {
				$hasOrderTransferAmount = $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['ebay深圳仓']);
				$hasOrderTransferAmount += $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']);
				$canTransferQuantity = $warehouseAvailableStock - $productThirtyDaySales - $hasOrderTransferAmount;

			} elseif ($transferFromWarehouse == self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓']) {
				$hasOrderTransferAmount = $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['评测仓']);
				$hasOrderTransferAmount += $this->_transferApplyModel->canApplyQuantity($goodsSn, self::CAN_APPLY_TRANSFER_WAREHOUSE['电子仓'], self::CAN_APPLY_TRANSFER_WAREHOUSE['分销仓']);
				$canTransferQuantity = $warehouseAvailableStock - ($productThirtyDaySales * 0.5) - $hasOrderTransferAmount;
			}

			break;
		default:
			break;
		}
		//可调拨数量小于零时，表示不可调拨
		$canTransferQuantity = max(0, $canTransferQuantity);
		return $canTransferQuantity;
	}
}